package springboot.dao;

import springboot.domain.ReportRepair;
import springboot.domain.StudentReportRepair;
import springboot.Interface.daoInterface.ReportRepairManagementDaoInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Repository
public class ReportRepairDao implements ReportRepairManagementDaoInterface {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    //查找所有宿管已批准的报修申请
    public Collection<ReportRepair> findAll(int pageNo, int pageSize){
        int offset = (pageNo-1)*pageSize;
        String pagnation= " Limit " + offset + "," + pageSize;
        String sql = "SELECT * FROM 报修处理"+pagnation;
        RowMapper<ReportRepair> rowMapper = new BeanPropertyRowMapper<ReportRepair>(ReportRepair.class);
        List<ReportRepair> reportRepairs = jdbcTemplate.query(sql, new Object[]{},rowMapper);
        return reportRepairs;
    }
    @Override
    //根据宿舍楼查找宿管已批准的报修申请
    public Collection<ReportRepair> find(String dorm){
        String sql = "SELECT * FROM 报修处理 WHERE dorm = ?";
        RowMapper<ReportRepair> rowMapper = new BeanPropertyRowMapper<ReportRepair>(ReportRepair.class);
        List<ReportRepair> reportRepairs = jdbcTemplate.query(sql, new Object[]{dorm},rowMapper);
        if (reportRepairs.size()>0) {
            return reportRepairs;
        } else {
            return null;
        }
    }
    @Override
    //查找需要宿管批准的学生报修申请
    public Collection<StudentReportRepair> checkReport(int pageNo,int pageSize){
        int offset = (pageNo-1)*pageSize;
        String pagnation= " Limit " + offset + "," + pageSize;
        String sql = "SELECT * FROM 报修处理申请"+pagnation;
        RowMapper<StudentReportRepair> rowMapper = new BeanPropertyRowMapper<StudentReportRepair>(StudentReportRepair.class);
        List<StudentReportRepair> studentReportRepairs = jdbcTemplate.query(sql, new Object[]{},rowMapper);
        return studentReportRepairs;
    }
    @Override
    //学生提交报修申请
    public boolean studentReportRepair(StudentReportRepair studentReportRepair){
        String sql = "INSERT INTO 报修处理申请 (dorm_no,no,name,report_time,report_reason,dorm)" +
                "VALUES"+"(?,?,?,?,?,?)";
        int a = jdbcTemplate.update(sql,studentReportRepair.getDorm_no(),
                studentReportRepair.getNo(),studentReportRepair.getName()
                , studentReportRepair.getReport_time(),studentReportRepair.getReport_reason(),
                studentReportRepair.getDorm());
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    @Override
    //宿管同意批准学生的报修申请
    public boolean managerAgreeReportRepair(StudentReportRepair studentReportRepair){
        String sql = "INSERT INTO 报修处理 (dorm_no,no,name,report_time,report_reason,handle_sta,dorm)" +
                "VALUES"+"(?,?,?,?,?,?,?)";
        int a = jdbcTemplate.update(sql,studentReportRepair.getDorm_no(),
                studentReportRepair.getNo(),studentReportRepair.getName()
                , studentReportRepair.getReport_time(),studentReportRepair.getReport_reason(),
                "已同意但未处理",studentReportRepair.getDorm());
        this.deleteStudentReportRepair(studentReportRepair.getId());
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    @Override
    public boolean managerDisAgreeReportRepair(StudentReportRepair studentReportRepair){
        String sql = "INSERT INTO 报修处理 (dorm_no,no,name,report_time,report_reason,handle_sta,dorm)" +
                "VALUES"+"(?,?,?,?,?,?,?)";
        int a = jdbcTemplate.update(sql,studentReportRepair.getDorm_no(),
                studentReportRepair.getNo(),studentReportRepair.getName()
                , studentReportRepair.getReport_time(),studentReportRepair.getReport_reason(),
                "未同意",studentReportRepair.getDorm());
        this.deleteStudentReportRepair(studentReportRepair.getId());
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    @Override
    //删除学生提交的不合格的报修申请
    public boolean deleteStudentReportRepair(int id){
        String sql = "delete from 报修处理申请 where id = ?";
        int a = jdbcTemplate.update(sql,id);
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }

    @Override
    //增加一条报修申请数据
    public boolean add(ReportRepair reportRepair){
        String sql = "INSERT INTO 报修处理 (dorm_no,no,name,report_time,report_reason,handle_sta,dorm)" +
                "VALUES"+ "(?,?,?,?,?,?,?)";
        int a = jdbcTemplate.update(sql,reportRepair.getDorm_no(),reportRepair.getNo(),reportRepair.getName()
        , reportRepair.getReport_time(),reportRepair.getReport_reason(),
                reportRepair.getHandle_sta(),reportRepair.getDorm());
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    @Override
    //删除一条报修申请数据
    public boolean delete(int id){
        String sql = "delete from 报修处理 where id = ?";
        int a = jdbcTemplate.update(sql,id);
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
    @Override
    //修改一条报修申请数据
    public boolean update(ReportRepair reportRepair){
        String sql = "update 报修处理 set dorm_no = ? , no = ? ,name = ?,report_time = ?," +
                "report_reason = ?,handle_sta = ?,dorm = ? where id = ?";
        int a = jdbcTemplate.update(sql,reportRepair.getDorm_no(),reportRepair.getNo(),reportRepair.getName()
                , reportRepair.getReport_time(),reportRepair.getReport_reason(),
                reportRepair.getHandle_sta(),reportRepair.getDorm(),reportRepair.getId());
        if (a != 1 ){
            return false;
        }else{
            return true;
        }
    }
}
